package vip.xiaonuo.biz.modular.approval.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.approval.service.BizWxService;

@Service
@Slf4j
public class BizWxServiceImpl implements BizWxService {
    @Autowired
    private WxMaService wxMaService;
    private static final String approvalTemplateId = "QgVDP0hqwxB-1fVU0c4x3v61a4MK-F1bj4TBPvg_U0I";
    private static final String appointmentTemplateId = "azCy0PXPy8Qz9rXH4vymhDJEja7fY3tZQRMQRLK4Ikc";

    public void sendApprovalTemplate(String thing8, String name1, String time2, String thing9, String thing28, String openid) {
        if (openid == null) {
            log.error("未找到openid");
            return;
        }
        WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
        subscribeMessage.addData(new WxMaSubscribeMessage.MsgData("thing8", thing8));//申请部门
        subscribeMessage.addData(new WxMaSubscribeMessage.MsgData("name1", name1));//申请人
        subscribeMessage.addData(new WxMaSubscribeMessage.MsgData("time2", time2));//申请时间
        subscribeMessage.addData(new WxMaSubscribeMessage.MsgData("thing9", thing9));//申请主题
        subscribeMessage.addData(new WxMaSubscribeMessage.MsgData("thing28", thing28));//当前审批人
        subscribeMessage.setTemplateId(approvalTemplateId);
        subscribeMessage.setToUser(openid);
        try {
            wxMaService.getSubscribeService().sendSubscribeMsg(subscribeMessage);
        } catch (WxErrorException e) {
            log.error("发送模板消息失败：{}", e.getMessage());
        }
    }


    public void sendAppointmentTemplate(String thing1, String time12, String thing8, String openid) {
        if (openid == null) {
            log.error("未找到openid");
            return;
        }
        WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
        subscribeMessage.addData(new WxMaSubscribeMessage.MsgData("thing1", thing1));//预约人
        subscribeMessage.addData(new WxMaSubscribeMessage.MsgData("time12", time12));//到访时间
        subscribeMessage.addData(new WxMaSubscribeMessage.MsgData("thing8", thing8));//温馨提示
        subscribeMessage.setTemplateId(appointmentTemplateId);
        subscribeMessage.setToUser(openid);
        try {
            wxMaService.getSubscribeService().sendSubscribeMsg(subscribeMessage);
        } catch (WxErrorException e) {
            log.error("发送模板消息失败：{}", e.getMessage());
        }
    }
}
